package pers.zh.parking.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.zh.parking.dao.EvaluationMapper;
import pers.zh.parking.dao.ParkingRecordMapper;
import pers.zh.parking.enums.RecordStatus;
import pers.zh.parking.po.Evaluation;
import pers.zh.parking.po.ParkingRecord;
import pers.zh.parking.utils.TimeUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ParkRecordService implements IParkRecordService {

    @Autowired
    ParkingRecordMapper recordMapper;
    @Autowired
    EvaluationMapper evaluationMapper;

    @Override
    public boolean insertRecord(ParkingRecord record) {
        record.setReserveTime(TimeUtils.date2String(new Date()));
        return recordMapper.insertSelective(record) > 0;
    }

    @Override
    public boolean hasReservedCurLot(Integer lotId, Integer userId) {
        ParkingRecord record = new ParkingRecord();
        record.setLotId(lotId);
        record.setUserId(userId);
        return recordMapper.selectReservedCount(record) > 0;
    }

    @Override
    public boolean cancelOrder(Integer recordId) {
        ParkingRecord record = new ParkingRecord();
        record.setRecordId(recordId);
        record.setStatus(RecordStatus.CANCEL.getStatus());
        Map<String, Object> map = new HashMap<>();
        map.put("record", record);
        return recordMapper.updateByPKey(map) > 0;
    }

    @Override
    public List<ParkingRecord> getReservations(long userId, int status) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("status", status);
        return recordMapper.getReservations(map);
    }

    @Override
    public boolean hasReservedOther(Integer userId) {
        ParkingRecord record = new ParkingRecord();
        record.setUserId(userId);
        return recordMapper.selectReservedCount(record) > 0;
    }

    @Override
    public boolean park(Integer recordId, String carNo) {
        ParkingRecord record = new ParkingRecord();
        record.setRecordId(recordId);
        record.setCarNo(carNo);
        record.setStatus(RecordStatus.PARKING.getStatus());
        record.setEntryTime(TimeUtils.date2String(new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("record", record);
        return recordMapper.updateByPKey(map) > 0;
    }

    @Override
    public boolean pay(Integer recordId) {
        ParkingRecord record = new ParkingRecord();
        record.setRecordId(recordId);
        record.setStatus(RecordStatus.LEAVED.getStatus());
        record.setLeaveTime(TimeUtils.date2String(new Date()));
        // 计费入库
        record.setFees(recordMapper.calFees(recordId));
        Map<String, Object> map = new HashMap<>();
        map.put("record", record);
        recordMapper.updateByPKey(map);
        // 写入一条未评价状态的记录
        Evaluation evaluation = new Evaluation();
        evaluation.setEvalStatus(1);
        evaluation.setRecordId(recordId);
        evaluationMapper.insertSelective(evaluation);
        return true;
    }

    @Override
    public boolean eval(Evaluation evaluation) {
        ParkingRecord record = new ParkingRecord();
        record.setRecordId(evaluation.getRecordId());
        record.setStatus(5);
        Map<String, Object> map = new HashMap<>();
        map.put("record", record);
        recordMapper.updateByPKey(map);
        evaluation.setEvalStatus(2); // 已评价
        return evaluationMapper.update(evaluation) > 0;
    }

    @Override
    public List<ParkingRecord> getEvals(Integer userId) {
        return recordMapper.getEvals(userId);
    }


}
